package multithread.framework.xml;

import java.util.List;

import multithread.framework.constant.XMLConfigConstant;
import multithread.framework.threadmethod.ThreadMethodContainer;

import org.jdom.Comment;
import org.jdom.Element;

public class CaseElement extends Element implements IVerifiable {
	private static final long serialVersionUID = 6055735512186152386L;

	public CaseElement(ThreadMethodContainer container) {
		super(XMLConfigConstant.CASE_ELEMENT);
		init(container.getTestCaseClass(), container.getThreadMethodTimeout());
	}

	public CaseElement(Class<?> testCaseClz, long timeout) {
		super(XMLConfigConstant.CASE_ELEMENT);
		init(testCaseClz, timeout);
	}

	private void init(Class<?> caseClz, long timeout) {
		Element clzElement = new Element(XMLConfigConstant.CLASS_ELEMENT);
		clzElement.setText(caseClz.getName());
		addContent(clzElement);
		if (timeout > 0) {
			Element timeoutElement = new Element(
					XMLConfigConstant.TIMEOUT_ELEMENT);
			timeoutElement.setText(String.valueOf(timeout));
			addContent(timeoutElement);
		}
	}

	public Element addGroupElement(GroupElement group) {
		if (group != null && group.validate()) {
			return addContent(group);
		}
		return null;
	}

	public Element addCommonElement(CommonElement common) {
		if (common != null && common.validate()) {
			return addContent(common);
		}
		return null;
	}

	public Element addComment(String comment) {
		if (validate()) {
			return addContent(new Comment(comment));
		}
		return null;
	}

	@Override
	public boolean validate() {
		List<?> childlist = this.getChildren();
		return childlist == null ? false : childlist.size() > 1 ? true : false;
	}

}